package array

func sortedSquares(nums []int) []int {
	n := len(nums)
	res := make([]int, n)
	i, j := 0, n - 1
	for pos := n - 1; pos >= 0; pos-- {
		if v, w := nums[i] * nums[i], nums[j] * nums[j]; v > w {
			res[pos] = v
			i++
		} else {
			res[pos] = w
			j--
		}
	}
	return res
}